using Microsoft.AspNetCore.Mvc;
using JTZY.Model.Order.Dto;
using JTZY.Model.Order;
using JTZY.Service.Order.IOrderService;
using JTZY.Admin.WebApi.Filters;
using Org.BouncyCastle.Tls;

//创建时间：2024-10-28
namespace JTZY.Admin.WebApi.Controllers.Order
{
    /// <summary>
    /// 订单明细表
    /// </summary>
    [Verify]
    [Route("order/Corecmsorderitem")]
    public class CorecmsorderitemController : BaseController
    {
        /// <summary>
        /// 订单明细表接口
        /// </summary>
        private readonly ICorecmsorderitemService _CorecmsorderitemService;

        public CorecmsorderitemController(ICorecmsorderitemService CorecmsorderitemService)
        {
            _CorecmsorderitemService = CorecmsorderitemService;
        }

        /// <summary>
        /// 查询订单明细表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "corecmsorderitem:list")]
        public IActionResult QueryCorecmsorderitem([FromQuery] CorecmsorderitemQueryDto parm)
        {
            var response = _CorecmsorderitemService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询订单明细表详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "corecmsorderitem:query")]
        public IActionResult GetCorecmsorderitem(int Id)
        {
            var response = _CorecmsorderitemService.GetInfo(Id);
            
            var info = response.Adapt<CorecmsorderitemDto>();
            return SUCCESS(info);
        }


        [HttpGet("getorderproducts")]
        [ActionPermissionFilter(Permission = "corecmsorderitem:query")]
        public IActionResult GetOrderProducts(string orderId)
        {
            long userId= HttpContext.GetUId();
            var response = _CorecmsorderitemService.GetInfo(orderId, userId);

            var info = response.Adapt<List<CorecmsorderitemDto>>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加订单明细表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "corecmsorderitem:add")]
        [Log(Title = "订单明细表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCorecmsorderitem([FromBody] CorecmsorderitemDto parm)
        {
            var modal = parm.Adapt<Corecmsorderitem>().ToCreate(HttpContext);

            var response = _CorecmsorderitemService.AddCorecmsorderitem(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 导出订单明细表
        /// </summary>
        /// <returns></returns>
        [Log(Title = "订单明细表", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "corecmsorderitem:export")]
        public IActionResult Export([FromQuery] CorecmsorderitemQueryDto parm)
        {
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _CorecmsorderitemService.ExportList(parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "订单明细表", "订单明细表");
            return ExportExcel(result.Item2, result.Item1);
        }

    }
}